package com.example.source_share.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.source_share.mapper.FileMapper;
import com.example.source_share.mapper.MajorMapper;
import com.example.source_share.mapper.UserMapper;
import com.example.source_share.pojo.Files;
import com.example.source_share.pojo.Majors;
import com.example.source_share.service.user.account.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
public class SearchController { //文件和用户的搜索API
    @Autowired
    UserMapper userMapper;
    @Autowired
    private FileMapper fileMapper;
    @Autowired
    private InfoService infoService;
    @PostMapping("/file/search")
    public List<Files> searchFile(@RequestParam Map<String,String> map) {
        //四个参数
        //filename
        //major
        //subject
        //uploader
        QueryWrapper<Files> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("filename",map.get("filename"))
                .like("majorid",map.get("major"))
                .like("subject",map.get("subject"))
                .like("author",map.get("uploader"));
        //System.out.println(fileMapper.selectList(queryWrapper));
        return fileMapper.selectList(queryWrapper);
    }
    @Autowired
    private MajorMapper majorMapper;

    @GetMapping("/file/descriptions") //获取全部文件描述
    public List<String> allDescription() {
        List<String> result=new ArrayList<>();
        List<Files> files=fileMapper.selectList(null);
        for (Files file : files) {
            if (!result.contains(file.getDescription())) result.add(file.getDescription());
        }
        return result;
    }
    @GetMapping("/college/all") //返回所有学院列表
    public List<String> allCollege() {
        List<String> result=new ArrayList<>();
        List<Majors> majors=majorMapper.selectList(null);
        for (Majors major : majors) {
            if (!result.contains(major.getCollegename())) result.add(major.getCollegename());
        }
        return result;
    }
    @GetMapping("/major/{college}") //返回所有学院列表
    public List<String> allCollege(@PathVariable String college) {
        List<String> result=new ArrayList<>();
        List<Majors> majors=majorMapper.selectList(null);
        for (Majors major : majors) {
            if(Objects.equals(major.getCollegename(), college)) result.add(major.getMajorname());
        }
        return result;
    }
}
